package com.kingyun.gpsinspection.purificationservice.services.som.innerservice.impl;

import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.FacadeEntityTypeMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.FacadeEntityType;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.FacadeEntityTypeService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * Created by yangcs on 2017/2/21.
 */
public class FacadeEntityTypeServiceImpl implements FacadeEntityTypeService {

    @Autowired
    private FacadeEntityTypeMapper facadeEntityTypeMapper;

    /**
     * 获取设备类型树
     * @return
     */
    @Override
    public List<FacadeEntityType> getDeviceEntityType(long updateTime, String orgId) {
        Map map = new HashMap<>();
        map.put("date",new Date(updateTime));
        map.put("orgId",orgId);
        int result = facadeEntityTypeMapper.getChange(map);
        // 表示有变动
        if( result > 0 ){
            // 获取设备级别的生产实体类型
            List<FacadeEntityType> list = facadeEntityTypeMapper.getEntityTypesByCode(orgId);
            List<FacadeEntityType> childs = null;
            if ( list != null && list.size() > 0 ){
                childs = new ArrayList<>();
                for ( FacadeEntityType facadeEntityType : list ){
                    facadeEntityType.setChildren(getChildren(facadeEntityType.getEntityTypeId()));
                    childs.add(facadeEntityType);
                }
            }
            return childs;
        }else{
            return null;
        }
    }

    /**
     * 查询子节点
     * @param entityTypeId
     * @return
     */
    public List<FacadeEntityType> getChildren(String entityTypeId){
        List<FacadeEntityType> list = facadeEntityTypeMapper.getChildren(entityTypeId);
        List<FacadeEntityType> children = null;
        if( list != null && list.size() > 0 ){
            children = new ArrayList<>();
            for ( FacadeEntityType type : list ){
                type.setChildren( getChildren(type.getEntityTypeId()) );
                children.add(type);
            }
        }
        return children;
    }

    /**
     * 获取工作内容类型树
     * @return
     *//*
    @Override
    public List<FacadeEntityType> getCellEntityType(long updateTime) {
        Map map = new HashMap<>();
        map.put("date",new Date(updateTime));
        map.put("code","1");
        int result = facadeEntityTypeMapper.getChange(map);
        // 表示有变动
        if( result > 0 ){
            return setChildren(facadeEntityTypeMapper.getEntityTypesByCode("1"));
        }else{
            return null;
        }
    }*/

}
